package com.a3dweijing.wetouch2.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.Util;
import com.a3dweijing.wetouch2.adapter.LocationListAdapter;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.User;
import com.a3dweijing.wetouch2.bean.UserLocationMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Yxd on 2018/3/26.
 */

public class LocationListActivity extends BaseActivity implements com.a3dweijing.wetouch2.callback.OnClick
{
    
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.location_list_add)
    TextView locationListAdd;
    
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    
    @BindView(R.id.location_list_rec)
    RecyclerView locationListRec;
    
    static final int RESULT = 21;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
    
    private void initView()
    {
        topText.setText("收货地址");
        locationListRec.setLayoutManager(new LinearLayoutManager(getBaseContext()));
    }
    
    private void initData()
    {
        OkGo.post("http://admin.3dweijing.com/admin.php/api/shopping/addressList")
            .tag(getBaseContext())
            .params("user_id", Singleton.getInstance().getUser().getUser_id())
            .execute(new StringCallback()
            {
                @Override
                public void onSuccess(String s, Call call, Response response)
                {
                    UserLocationMsg msg = new Gson().fromJson(s, UserLocationMsg.class);
                    if (msg.isFlag())
                    {
                        if (msg.getAddressList() != null && msg.getAddressList().size() > 0)
                        {
                            resautStatus.setVisibility(View.GONE);
                            locationListRec
                                .setAdapter(new LocationListAdapter(LocationListActivity.this, msg.getAddressList()));
                        }
                        else
                        {
                            resautStatus.setVisibility(View.VISIBLE);
                        }
                    }
                    else
                    {
                        resautStatus.setVisibility(View.VISIBLE);
                    }
                    
                }
            });
    }
    
    public static void startActivityForResult(Activity context, int what)
    {
        if (Util.isFastClick())
        {
            Intent intent = new Intent();
            intent.setClass(context, LocationListActivity.class);
            context.startActivityForResult(intent, what);
        }
    }
    
    @OnClick({R.id.top_right, R.id.location_list_add, R.id.back})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.top_right:
                finish();
                break;
            case R.id.location_list_add:
                LocationAddActivity.startActivityForResult(LocationListActivity.this, RESULT);
                break;
            case R.id.back:
                finish();
        }
    }
    
    @Override
    public void onclick(int position, Object o)
    {
        UserLocationMsg.AddressBean bean = (UserLocationMsg.AddressBean)o;
        switch (position)
        {
            case R.id.location_user_delete:
                deleteLocation(bean.getAddress_id() + "");
                
                break;
            default:
                Intent intent = getIntent();
                intent.putExtra("addressbean", bean);
                setResult(BuyActivity.LOCATIONRESULT, intent);
                finish();
                break;
        }
        
    }
    
    private void deleteLocation(String id)
    {
        OkGo.post("http://admin.3dweijing.com/admin.php/api/shopping/deleteAddress")
            .params("address_id", id)
            .tag(LocationListActivity.this)
            .execute(new StringCallback()
            {
                @Override
                public void onSuccess(String s, Call call, Response response)
                {
                    Log.e("--", "onSuccess: " + s);
                    DeleteLocationMsg msg = new Gson().fromJson(s, DeleteLocationMsg.class);
                    if (msg.isFlag())
                    {
                        initData();
                    }
                    else
                    {
                        Toast.makeText(LocationListActivity.this, msg.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
    }
}
